<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
<!--    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>-->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/register.css" />
</head>
<body>
<div class="box">
    <form>
        <div class="register-field">
            <input type="text" id="name" required="">
            <div class="underline"></div>
            <label>用户</label>
        </div>
        <div class="register-field">
            <input type="password" id="password" required="">
            <div class="underline"></div>
            <label>密码</label>
        </div>
        <div class="register-field">
            <input type="password" id="password2" required="">
            <div class="underline"></div>
            <label>确认密码</label>
        </div>
        <div class="register">
            <input type="button" value="注册" onclick="send()">
        </div>
    </form>
</div>


</body>
<script>
    function send() {
        var name = $("#name").val();
        var password = $("#password").val();
        var password2 = $("#password2").val();
        var userinfo = {
            'name' : name,
            'password' : password
        };
        //简单的注册验证
        if (name == "") {
            alert("用户名不能为空");
            document.getElementById("name").focus();//获取焦点
        } else if (password == "") {
            alert("密码不能为空");
            document.getElementById("password").focus();
        }
        else if(password.length<6){
            alert("密码位数不能小于6位！");
            document.getElementById("password").focus();
        }
        else if(password != password2){
            alert("两次密码输入不一致，请重新确认！");
            document.getElementById("password2").focus();
        }
        // if ($("#password").val() == $("#password2").val() && $("#password").val().length >= 6)
        else
        {
            $.ajax({
                    url:'/insert',
                    type:'POST',
                    dataType:'json',
                    contentType:'application/json',
                    data:JSON.stringify(userinfo),
                    success:function (data) {
                        if (data)
                        {
                            alert('注册成功');
                            toIndex();
                        }
                        else
                        {
                            alert('账号已存在,注册失败')
                        }
                    }
                }
            )
        }
    }
    function toIndex() {
        window.open('/index');
    }
</script>
</html>
